    //判断用户是否登录
function judgeLogin() {
    let cookie = document.cookie.split(';')
    let cookieObj = cookie.reduce((pre, next) => {
        const key = next.split('=')[0]
        const val = next.split('=')[1]
        pre[key] = val
        return pre
    }, {})
    // console.log(cookieObj)
    if(!cookieObj.username) {
        window.location.href = './login.html'
        return
    }
    return cookieObj
}

let loginMessage = judgeLogin()


//退出登录
function quitLogin() {
    let time = new Date()
    time.setTime(time.getTime() - 1000 * 60 * 60 * 8 - 1000)
    document.cookie = 'username=0;expires=' + time
    window.location.href = './login.html'
}

//小购物车渲染
function applyLitterCart() {
	$.ajax({
		url: '/cart',
		type: 'POST',
		dataType: 'json',
		data: {username: loginMessage.username},
		success: function(res) {
			littleCart(res)
		}
	})
}
function littleCart(json) {
	//渲染小购物车商品
	let str1 = '',
		num = 0,
		sum = 0
	json.forEach((item) => {
		num += item.num * 1
		sum += item.num * item.price_info.replace(/[^0-9]/ig,'')
		str1 += `<li>
		<span class="thumb fl"><img src="${item.img}" alt=""></span>
		<span class="goname fl">
			${item.title}
		</span>
		<span class="goprice">
			${item.price_info} × ${item.num}
		</span>
	</li>`
	})
	let str = `<ul class="clearfix">${str1}</ul><div class="fl jiesuan clearfix">
	<i class="gongsanjian">共<em>${num}</em>件商品
		<i><em>${sum}</em>元</i>
	</i>
	<a class="lalala" href="javascript:;">去购物车结算</a>
</div>`

	$('.shop-h').html(str)

}